<?php
/*
 * Dinglf1994
 * mongo连接测试
 * */
namespace mongo\index\controller;
use think\Controller;

class Mongo extends Controller
{
    protected static $linkMongo = null;
    public function newMongo()
    {
        if(self::$linkMongo === null)
        {
            //$MongoDb = new \MongoClient('mongodb://119.254.210.77:58460');
            $MongoDb = new \MongoClient();
            self::$linkMongo = $MongoDb;
            return self::$linkMongo;
        }
        else
        {
            return self::$linkMongo;
        }
    }
    function initMongodb($id,$name) //是否初始化mongodb中的用户属性表
    {
        $mongo = $this->newMongo();
        $mongo_ini = $mongo->xpai->Uproperty;
        $mFind = array(
            "uid" => "$id",
        );
        $sel = $mongo_ini->findOne($mFind);
        if($sel)
        {
            //$this->answerWeiXin('feng', 'FindOne连接出问题');
        }
        else
        {
            $inputData = array(
                "uid" => "$id",
                "nickname" => "$name",
                "pic" => "1.jpg",
                "area" => "北京",
                "like" => "正常",
            );
            $mongo_ini->save($inputData);
        }
        $mongo->close();
    }
    public function index()
    {
        return $this->fetch('mongo');
    }
    public function linkMongo(){
        $id = $_POST['id'];
        $name = $_POST['nickname'];
        $this->initMongodb($id, $name);
        $mongo = $this->newMongo();
        $mongo_uProperty = $mongo->xpai->Uproperty;
        $mongo_answer = $mongo->xpai->answer;
        $mongo->close();
        echo "用户 $name 连接成功";
    }
}
